//
//  AssociationsViewController.swift
//  RunTimeDemo
//
//  Created by YYPan on 2018/5/24.
//  Copyright © 2018年 YYPan. All rights reserved.
//

import UIKit

class AssociationsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
		view.backgroundColor = UIColor.white
		buttonDemo()
		tapGestureDemo()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
	// MARK:- 更改按钮的响应区域
	private func buttonDemo() {
		let button = UIButton(frame: CGRect(x: 0, y: 150, width: 200, height: 100))
		button.center.x = view.center.x
		button.backgroundColor = UIColor.red
		button.setTitle("按钮实际大小", for: .normal)
		button.setTitleColor(UIColor.white, for: .normal)
		button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
		
		/* 将按钮的响应区域按照四个方向分别增加50 */
		button.setEdge(top: 50, right: 50, bottom: 50, left: 50)
		view.addSubview(button)
		
		/* 虚线内为按钮的响应区域 */
		let shaperLayer = CAShapeLayer()
		let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: button.frame.size.width + 100, height: button.frame.size.height + 100))
		shaperLayer.path = path.cgPath
		shaperLayer.position = CGPoint(x: button.frame.origin.x - 50, y: button.frame.origin.y - 50)
		shaperLayer.fillColor = UIColor.clear.cgColor
		shaperLayer.strokeColor = UIColor.red.cgColor
		shaperLayer.lineWidth = 2
		shaperLayer.lineDashPattern = [3,3]
		view.layer.addSublayer(shaperLayer)
	}
	
	@objc private func buttonClick() {
		print("buttonClick")
	}
	
	// MARK:- 防止手势的暴力点击(控制手势点击的响应时间间隔)
	private func tapGestureDemo() {
		let tapView = UIView(frame: CGRect(x: 0, y: 450, width: 300, height: 100))
		tapView.backgroundColor = UIColor.blue
		tapView.center.x = view.center.x
		view.addSubview(tapView)
		
		let label = UILabel(frame: tapView.bounds)
		label.text = "控制手势点击的响应时间间隔"
		label.textColor = UIColor.white
		label.textAlignment = .center
		tapView.addSubview(label)
		
		/* 初始化时采用新增的初始化方式,设置响应间隔 */
		let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick), duration: 5.0)
		tapView.addGestureRecognizer(tap)
	}
	
	@objc private func tapClick() {
		print("tapclick")
	}
}
